package me.qu42.zpage.net;

import me.qu42.zpage.data.*;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JsonResolver {

    public static UserInformation checkIdByEmail(String json){
        try {
            JSONObject jsonObject = new JSONObject(json);
            
            UserInformation info = new UserInformation();
            
            info.following_num = jsonObject.getInt("following_num");
            info.self_intro = jsonObject.getString("self_intro");
            info.ico = jsonObject.getString("ico");
            info.name = jsonObject.getString("name");
            info.follower_num = jsonObject.getInt("follower_num");
            info.marry = jsonObject.getInt("marry");
            info.place_home = jsonObject.getLong("place_home");
            info.pic = jsonObject.getString("pic");
            info.place_now = jsonObject.getLong("place_now");
            info.moto = jsonObject.getString("moto");
            info.user_link = jsonObject.getString("user_link");
            info.user_id = jsonObject.getLong("user_id");
            info.sex = jsonObject.getInt("sex");
            info.verify_state = jsonObject.getInt("verify_state");
            info.place_now_name = jsonObject.getString("place_now_name");
            info.place_home_name = jsonObject.getString("place_home_name");
            
            return info;
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
            
        }
    }
    
    public static String[] newLoginAuth(String json){
        try {
            JSONObject jsonObject = new JSONObject(json);
            String aT = jsonObject.getString("access_token");
            String rT = jsonObject.getString("refresh_token");
            String[] array;
            array = new String[2];
            array[0] = aT;
            array[1] = rT;
            return array;
        } catch (Exception e) {
            return null;
        }
    }
    
    public static PostInformation[] timeline(String json){
        PostInformation[] postInfo;
        try{
            
            JSONObject jsonObject = new JSONObject(json);
            
            JSONArray infoArray = jsonObject.getJSONArray("items");
            postInfo  = new PostInformation[infoArray.length()];
            Log.w("length",infoArray.length()+"");
            
            PostInformation info;
            for(int i=0;i<infoArray.length();i++){
                JSONObject currentObject = (JSONObject)infoArray.get(i);
                info = new PostInformation();
                info.cid = currentObject.getInt("cid");
                info.user_name = currentObject.getString("user_name");
                info.user_link = currentObject.getString("user_link");
                //info.rt_list = currentObject.getString("rt_list");
                //info.user_id = currentObject.getLong("user_id");
                info.reply_count = currentObject.getLong("reply_count");
                info.timestamp = currentObject.getLong("timestamp");
                info.name = currentObject.getString("name");
                info.vote_state = currentObject.getBoolean("vote_state");
                info.vote = currentObject.getLong("vote");
                info.txt = currentObject.getString("txt");
                info.icon = currentObject.getString("icon");
                postInfo[i] = info;
                Log.w("title",info.name);
            }
            
            return postInfo;
            
        } catch (Exception e) {
            e.printStackTrace();
            
            return null;
        }
        
        
    }
}
